<script setup lang="ts">
import { poleDelApi, poleGetAreaListApi, poleGetListApi } from '@/apis/rod'
import type { ListParams } from '@/types'
import type { PoleI } from '@/types/rod'
import { ElMessage, ElMessageBox } from 'element-plus'
import { onMounted, ref } from 'vue'
import PoleDialog from './components/PoleDialog.vue'
import type { RoleI } from '@/types/system'

const listParams = ref<ListParams>({
  page: 1,
  pageSize: 8,
  poleName: '',
  poleNumber: '',
  poleStatus: undefined,
})
const list = ref<PoleI[]>()
const total = ref(0)
const getList = async () => {
  const res = await poleGetListApi(listParams.value)
  list.value = res.rows
  total.value = res.total
}
getList()
const onSearch = () => {
  listParams.value.page = 1
  listParams.value.pageSize = 8
  getList()
}
const changePage = (page: number) => {
  listParams.value.page = page
  getList()
}
const changePageSize = (pageSize: number) => {
  listParams.value.page = 1
  listParams.value.pageSize = pageSize
  getList()
}
const onDel = async (ids: string) => {
  await poleDelApi(ids)
  ElMessage.success('删除成功~')

  if (list.value?.length === 1 && listParams.value.page > 1) {
    listParams.value.page--
  }

  getList()
}
let ids: string = ''
const handleSelectionChange = (rows: any[]) => {
  ids = rows.map((item) => item.id).join(',')
}
const onDelList = async () => {
  await ElMessageBox.confirm('您确定删除所有选中的数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  await poleDelApi(ids)
  ElMessage.success('删除成功~')
  const arr = ids.split(',')

  if (arr.length === list.value?.length && listParams.value.page > 1) {
    listParams.value.page--
  }

  getList()
}
/**
 * 弹层
 */
const dialogVisible = ref(false)
let areaList: PoleI[] = []
onMounted(async () => {
  // 获取关联区域下拉列表
  areaList = await poleGetAreaListApi()
})
let dialogParams: PoleI = {
  id: undefined,
  poleName: '',
  poleNumber: '',
  poleIp: '',
  areaId: undefined,
  poleType: '',
}
const openDialog = (row: PoleI) => {
  dialogParams = { ...row, areaName: undefined, poleStatus: undefined }
  dialogVisible.value = true
}
</script>

<template>
  <div class="page-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="listParams">
      <el-form-item label="一体杆名称:">
        <el-input
          placeholder="请输入一体杆名称"
          style="width: 180px"
          v-model="listParams.poleName"
        />
      </el-form-item>
      <el-form-item label="一体杆编号:">
        <el-input
          placeholder="请输入一体杆编号"
          style="width: 180px"
          v-model="listParams.poleNumber"
        />
      </el-form-item>
      <el-form-item label="运行状态:">
        <el-select
          placeholder="请选择运行状态"
          style="width: 180px"
          v-model="listParams.poleStatus"
        >
          <el-option label="正常" :value="0"></el-option>
          <el-option label="异常" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <div style="margin: 20px 0">
      <el-button type="primary" @click="dialogVisible = true"
        >添加一体杆</el-button
      >
      <el-button @click="onDelList">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <el-table :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="30" />
      <el-table-column label="序号" type="index" align="center" width="70" />
      <el-table-column label="一体杆名称" align="center" prop="poleName" />
      <el-table-column label="一体杆编号" align="center" prop="poleNumber" />
      <el-table-column label="一体杆IP" align="center" prop="poleIp" />
      <el-table-column label="安装区域" align="center" prop="areaName" />
      <el-table-column label="一体杆类型" align="center" prop="poleType">
        <template #default="{ row }">
          {{ row.poleType === 'entrance' ? '入口' : '出口' }}
        </template>
      </el-table-column>
      <el-table-column label="运行状态" align="center" prop="poleStatus">
        <template #default="{ row }">
          <el-tag :type="row.poleStatus === 0 ? 'success' : 'danger'">
            {{ row.poleStatus === 0 ? '正常' : '异常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="openDialog(row)">编辑</el-button>
          <el-popconfirm title="确定删除吗?" @confirm="onDel(row.id)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="listParams.page"
      v-model:page-size="listParams.pageSize"
      layout="total, prev, pager, next, slot, jumper"
      :total="total"
      @current-change="changePage"
    >
      <el-select
        style="width: 95px"
        v-model="listParams.pageSize"
        @change="changePageSize"
      >
        <el-option label="8条/页" :value="8" />
        <el-option label="10条/页" :value="10" />
        <el-option label="15条/页" :value="15" />
      </el-select>
    </el-pagination>
    <!-- 弹层区域 -->
    <pole-dialog
      v-model:dialogVisible="dialogVisible"
      v-model:dialogParams="dialogParams"
      :areaList="areaList"
      @success="getList"
    />
  </div>
</template>

<style lang="scss" scoped>
.page-container {
  padding: 20px;

  .el-form {
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);

    ::v-deep(.el-form-item__label) {
      color: rgb(39, 39, 39);
      font-weight: 500;
    }
  }

  .el-table {
    width: 100%;
    height: 400px;

    ::v-deep(.el-table__header th) {
      background-color: rgb(237, 237, 237, 0.9);
      color: rgb(43, 37, 37);
      padding: 10px 0;
    }

    ::v-deep(.el-table__body tr) {
      height: 50px;
    }
  }

  .el-pagination {
    display: flex;
    justify-content: flex-end;
    padding: 20px 0;
  }
}
</style>
